package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.GameScene;
import com.handinfo.uimenu.AbsHyperLinker;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;

public class UIGonglue extends UIAbstractWindow
{
	private final int STATE_ITEM = 1;
	private final int STATE_DETAIL = 2;

	private final int W_LABEL = (AbstractGameScreen.FONT_W << 1) + 4;
	private final int RW_LABEL = (W_LABEL + UITools.IW_RECTS);
	private final int H_RECT = UITools.ENDY_CONTENT - UITools.INITY_CONTENT - (UITools.H_LABEL_STR + UITools.IH_RECTS);
	//
	private String[] m_labels;//标签
	private String[][] m_items;//标签选项
	private AbsHyperLinker[][] m_detail;//详细内容

	private int X_ARR_LEFT;
	private int X_LABEL_FIRST;
	private int X_ARR_RIGHT;
	private int NUM_LIST_LABELS = 3;// 一页显示多少条标签
	private int NUM_ITEMS = 3;// 一页显示多少条Item
	//
	private int m_index_topItem;
	private int m_index_first_label;
	private int m_index_label;

	public UIGonglue(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
	}

	private void initDis()
	{
		NUM_LIST_LABELS = ((UITools.W_RECT - ((UIManager.IMAGE_XINGJING.getWidth() >> 1) << 1) + UITools.IW_RECTS) / (W_LABEL + UITools.IW_RECT_CONTENT));
		NUM_LIST_LABELS = NUM_LIST_LABELS > m_labels.length ? m_labels.length : NUM_LIST_LABELS;
		X_LABEL_FIRST = ((UITools.W_BG - (NUM_LIST_LABELS * RW_LABEL - UITools.IW_RECTS)) >> 1);
		X_ARR_LEFT = X_LABEL_FIRST - (UIManager.IMAGE_LABEL_ARR.getWidth()) - 2;
		X_ARR_RIGHT = UITools.W_BG - X_ARR_LEFT - (UIManager.IMAGE_LABEL_ARR.getWidth());
		
		NUM_ITEMS = (H_RECT - (UITools.IH_RECT_CONTENT<<1) + AbstractGameScreen.FONT_H - 1)/AbstractGameScreen.FONT_H ;
	}

	public static void recvMessage(DataInputStream dis) throws IOException
	{
		UIGonglue gonglue = (UIGonglue) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_GONGLUE);
		if (gonglue != null) {
			return;
		}
		int num_label = dis.readInt();
        //Tools.debugPrintln("num_label="+num_label);
		if (num_label <= 0) {
			Tools.debugPrintln("标签数据错误  ");
			return;
		}
		String[] labels = new String[num_label];
		String[] infos = new String[num_label];
		for (int i = 0; i < num_label; i++) {
			labels[i] = dis.readUTF();
			infos[i] = dis.readUTF();
		}

		String[][] items = new String[num_label][];
		AbsHyperLinker[][] details = new AbsHyperLinker[num_label][];

		final int W_DETAIL = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1);
		final int H_DETAIL = UITools.ENDY_CONTENT - UITools.INITY_CONTENT - (UITools.H_LABEL_STR + UITools.IH_RECTS);
		final String endTag = "</page>";
		final int len = endTag.length();
		final String startTag = "<page";
		final char equip = '=';
		final char endChar = '>';

		for (int i = 0; i < num_label; i++) {
			String info = infos[i];
			//Tools.debugPrintln("info["+i+"]="+infos[i]);
			int index = -1;
			int startIndex = 0;
			if ((index = info.indexOf(endTag, startIndex)) == -1) {
				details[i] = new AbsHyperLinker[1];
				details[i][0] = AbsHyperLinker.createHyperLinker(info, W_DETAIL, 0);
				details[i][0].setH(H_DETAIL);
			}
			else {
				ArrayList list = new ArrayList();
				while ((index = info.indexOf(endTag, startIndex)) != -1) {
					//Tools.debugPrintln("i="+i+";index="+index+";startIndex="+startIndex);
					index += len;
					int pageIndex = info.indexOf(startTag, startIndex);
					//Tools.debugPrintln("i="+i+";pageIndex="+pageIndex);
					int midIndex = info.indexOf(endChar, pageIndex) + 1;
					//Tools.debugPrintln("i="+i+";midIndex="+midIndex);
					String info1 = info.substring(pageIndex, midIndex);//startTag
					//Tools.debugPrintln("i="+i+";info1="+info1);
					String info3 = info1.substring(info1.indexOf(equip) + 1, info1.length() - 1);
					//Tools.debugPrintln("i="+i+";info3="+info3);
					int endIndex = info.indexOf(endTag, startIndex);
					//Tools.debugPrintln("i="+i+";endIndex="+endIndex);
					String info2 = info.substring(midIndex, endIndex);
					//Tools.debugPrintln("i="+i+";info2="+info2);
					list.addElement(info3);
					list.addElement(info2);
					//
					startIndex = index;
				}

				int size = (list.size() >> 1);
				if (size == 0) {
					items[i] = null;
					details[i] = null;
				}
				else {
					items[i] = new String[size];
					details[i] = new AbsHyperLinker[size];
					for (int j = 0; j < size; j++) {
						int tempIndex = j * 2;
						items[i][j] = (String) list.elementAt(tempIndex);
						details[i][j] = AbsHyperLinker.createHyperLinker((String) list.elementAt(tempIndex + 1),
								W_DETAIL, 0);
						details[i][j].setH(H_DETAIL);
					}
				}
			}

		}

		gonglue = (UIGonglue) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_GONGLUE);
		gonglue.m_labels = labels;
		gonglue.m_items = items;
		gonglue.m_detail = details;
		gonglue.initDis();
		gonglue.m_index = 0;
		gonglue.setLabelIndex(0);
		UIManager.getInstance().addWindow(gonglue);
	}
    
	public static void recvMessage2(DataInputStream dis) throws IOException
	{
		UIGonglue gonglue = (UIGonglue) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_GONGLUE);
		if (gonglue != null) {
			return;
		}
		int num_label = dis.readInt();
		if (num_label <= 0) {
			Tools.debugPrintln("标签数据错误  ");
			return;
		}
		String[] labels = new String[num_label];
		String[][] items = new String[num_label][];
		AbsHyperLinker[][] details = new AbsHyperLinker[num_label][];
		int W_DETAIL = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1);
		int H_DETAIL = UITools.ENDY_CONTENT - UITools.INITY_CONTENT - (UITools.H_LABEL_STR + UITools.IH_RECTS);
		for (int i = 0; i < num_label; i++) {
			labels[i] = dis.readUTF();
			switch (dis.readByte()) {
				case 0:
					String detail = dis.readUTF();

					details[i] = new AbsHyperLinker[1];
					details[i][0] = AbsHyperLinker.createHyperLinker(detail, W_DETAIL, 0);
					details[i][0].setH(H_DETAIL);
					break;
				case 1:
					int num_item = dis.readInt();
					if (num_item <= 0) {
						Tools.debugPrintln("数据错误  " + i);
						return;
					}
					items[i] = new String[num_item];
					details[i] = new AbsHyperLinker[num_item];

					for (int j = 0; j < num_item; j++) {
						items[i][j] = dis.readUTF();
						String temp = dis.readUTF();
						details[i][j] = AbsHyperLinker.createHyperLinker(temp, W_DETAIL, 0);
						details[i][j].setH(H_DETAIL);
					}
					break;
			}
		}
		gonglue = (UIGonglue) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_GONGLUE);
		gonglue.m_labels = labels;
		gonglue.m_items = items;
		gonglue.m_detail = details;
		gonglue.initDis();
		gonglue.m_index = 0;
		gonglue.setLabelIndex(0);
		UIManager.getInstance().addWindow(gonglue);
	}

	public static void test()
	{
		UIGonglue gonglue = new UIGonglue(-1);
		int W_DETAIL = UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1);
		int num_label = 15;
		gonglue.m_labels = new String[num_label];
		gonglue.m_items = new String[num_label][];
		gonglue.m_detail = new AbsHyperLinker[num_label][];
		for (int i = 0; i < gonglue.m_labels.length; i++) {
			gonglue.m_labels[i] = "你好";
			int num_item = (int) ((System.currentTimeMillis() / (i + 1001)) % 4) + 2;
			if (i == 0) {
				num_item = 0;
			}
			if (num_item == 0) {
				gonglue.m_items[i] = null;
				gonglue.m_detail[i] = new AbsHyperLinker[1];
				String temp = "<mark c=98ffff i=3827937197389>We are TestHello1</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello2</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello3</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello4</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello5</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello1</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello2</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello3</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello4</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl"
						+
						"<mark c=98ffff i=3827937197389>We are TestHello5</mark>dsajdlsajdjlasjldjaljdklsajkldjalkjdlasjl";
				gonglue.m_detail[i][0] = AbsHyperLinker.createHyperLinker(temp, W_DETAIL, 0);
//				gonglue.m_detail[i][0].setH(gonglue.m_detail[i][0].getRealHeight());
				gonglue.m_detail[i][0].setH(UITools.ENDY_CONTENT - UITools.INITY_CONTENT
						- (UITools.H_LABEL_STR + UITools.IH_RECTS));
			}
			else {
				gonglue.m_items[i] = new String[num_item];
				gonglue.m_detail[i] = new AbsHyperLinker[num_item];
				for (int j = 0; j < num_item; j++) {
					gonglue.m_items[i][j] = "Item[" + i + "][" + j + "]";
					gonglue.m_detail[i][j] = AbsHyperLinker.createHyperLinker("<mark c=987655>TestHello</mark>",
							W_DETAIL, 0);
					gonglue.m_detail[i][j].setH(gonglue.m_detail[i][j].getRealHeight());
				}
			}
		}
		gonglue.initDis();
		gonglue.m_index = 0;
		gonglue.setLabelIndex(0);
		UIManager.getInstance().addWindow(gonglue);
	}

	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "攻略");
		paintLabels(g);
		if (m_items == null || m_items.length <= 0) {
			return;
		}
		int labelIndex = getLabelIndex();
		if (labelIndex < 0 || labelIndex >= m_items.length) {
			return;
		}
		int tempX = m_x + UITools.INITX_CONTENT;
		int tempY = m_y + UITools.INITY_CONTENT + (UITools.H_LABEL_STR + UITools.IH_RECTS);
		int tempW = UITools.W_RECT;
		int tempH = H_RECT;
		UITools.paintContentRect(g, tempX, tempY, tempW, tempH);
		tempX += UITools.IW_RECT_CONTENT;
		tempY += UITools.IH_RECT_CONTENT;
		tempW -= (UITools.IW_RECT_CONTENT << 1);
		tempH -= (UITools.IH_RECT_CONTENT << 1);
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(tempX, tempY, tempW, tempH);
		//
		int itemIndex = getItemIndex();
		String[] items = m_items[labelIndex];
		switch (m_state) {
			case STATE_ITEM:
				int fosX = 0;
				int fosY = 0;
				for (int i = m_index_topItem; i < items.length; i++) {
					if (i == itemIndex) {
						g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS));
						fosX = tempX;
						fosY = tempY;
					}
					else {
						g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
					}
					g.drawString(items[i], tempX, tempY, 0);
					tempY += AbstractGameScreen.FONT_H;
				}
				UITools.drawSelectBox(g, fosX, fosY, UITools.W_CONTENT, AbstractGameScreen.FONT_H);
				break;
			case STATE_DETAIL:
			default:
				if (m_detail == null || m_detail.length <= 0) {
					return;
				}
				if (itemIndex < 0 || itemIndex >= m_detail[labelIndex].length
						|| m_detail[labelIndex][itemIndex] == null) {
					return;
				}
				AbsHyperLinker detail = m_detail[labelIndex][itemIndex];
				detail.paintInRect(g, tempX, tempY);
				break;
		}
		//
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintLabels(Graphics g)
	{
		int tempX = m_x + X_LABEL_FIRST;
		int tempY = m_y + UITools.INITY_CONTENT;
//		paintListLabelArr(g, m_x + X_ARR_LEFT, tempY, true);
		int labelIndex = getLabelIndex();
		for (int i = 0; i < NUM_LIST_LABELS; i++) {
			int index = m_index_first_label + i;
			if (index < 0) {
				continue;
			}
			if (index >= m_labels.length) {
				continue;
			}
			if (m_labels[index] == null) {
				continue;
			}
			UITools.paintLabelStr(g, tempX, tempY, W_LABEL, UITools.H_LABEL_STR, m_labels[i], index == labelIndex);
//			Tools.drawNumber(g, GameScene.s_img_num, tempX , tempY, UITools.W_NUM, UITools.H_NUM, i + 1, 0, Graphics.LEFT|Graphics.TOP, false);
			tempX += RW_LABEL;
		}
//		paintListLabelArr(g, m_x + X_ARR_RIGHT, tempY, false);
	}

	private void paintListLabelArr(Graphics g, int x, int y, boolean isLeft)
	{
		boolean isLight = true;
		if (isLeft) {
			isLight = true;
		}
		else {
			isLight = true;
		}
		UITools.paintLabelArr(g, x, y, isLeft, isLight);
	}

	private int getLabelIndex()
	{
		return m_index_label;
	}

	private void setLabelIndex(int index)
	{
		if (index < 0) {
			index = m_labels.length - 1;
		}
		else if (index >= m_labels.length) {
			index = 0;
		}
		m_index_label = index;
		if (m_index_label < m_index_first_label) {
			m_index_first_label = m_index_label;
		}
		else if (m_index_label - m_index_first_label >= NUM_LIST_LABELS) {
			m_index_first_label = m_index_label - NUM_LIST_LABELS + 1;
		}
		m_index_first_label = m_index_first_label < 0 ? 0 : m_index_first_label;
		setItemIndex(0);
		if (m_items[m_index_label] == null || m_items[m_index_label].length == 0) {
			setWindowStatus(STATE_DETAIL);
		}
		else {
			setWindowStatus(STATE_ITEM);
		}
	}

	private int getItemIndex()
	{
		return m_index;
	}

	private void setItemIndex(int index)
	{
		int labelIndex = getLabelIndex();
		if (m_items[labelIndex] == null) {
			return;
		}
		if (index < 0) {
			m_index = m_items[labelIndex].length -1 ;
			int temp =  m_index - NUM_ITEMS + 1 ;
			m_index_topItem = temp < 0 ? 0 : temp;
			return;
		}
		
		if (index >= m_items[labelIndex].length) {
			m_index = 0 ;
			m_index_topItem =  0;
			return;
		}
		this.m_index = index;
		if (m_index < m_index_topItem) {
			m_index_topItem = m_index;
		}
		else if (m_index >= m_index_topItem + NUM_ITEMS) {
			m_index_topItem = m_index - NUM_ITEMS;
		}
		m_index_topItem = m_index_topItem < 0 ? 0 : m_index_topItem;
	}

	protected void keyLogic()
	{
		switch (m_state) {
			case STATE_ITEM:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					closeGonglue();
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					setWindowStatus(STATE_DETAIL);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					setItemIndex(getItemIndex() - 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					setItemIndex(getItemIndex() + 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {
					setLabelIndex(getLabelIndex() - 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {
					setLabelIndex(getLabelIndex() + 1);
				}
				else {
					shortcut();
				}
				break;
			case STATE_DETAIL:
			default:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					closeGonglue();
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {
					setLabelIndex(getLabelIndex() - 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {
					setLabelIndex(getLabelIndex() + 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					AbsHyperLinker hyperLinker = m_detail[getLabelIndex()][getItemIndex()];
					int index = hyperLinker.actionOnFire();
					char type = hyperLinker.getHyperLinkTypes()[index];
					final String id = hyperLinker.getHyperLinkIds()[index];
					switch (type) {
						case AbsHyperLinker.URL_MAP://只有传送
							GameManager.getInstance().m_game_message.sendSceneTransfer(id);
							break ;
						case AbsHyperLinker.URL_NPC:
						case AbsHyperLinker.URL_PLAYER:
						case AbsHyperLinker.URL_ENEMY:
							final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(
									UIWindowFactory.WINDOW_POPMENU);
							popmenu.setPopmenus(new String[] { 	"自动寻路", "直接传送", "返回" });
						 
							popmenu.addListener(new UIListener() {

								public void actionPerformed()
								{
									if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
											|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
											|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY5)) {
										switch (popmenu.getCurrentIndex()) {
											case 0://寻路
												GameManager.getInstance().m_game_message.sendScenePathfinding(id);
												break;
											case 1://传送
												transform(id);
												break;
										}
										UIManager.getInstance().removeWindow(popmenu);
									}
									else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
										UIManager.getInstance().removeWindow(popmenu);
									}
								}
							});
							break;
						case AbsHyperLinker.URL_WINDOW:
							if (id.equals("bag")) {
								UIBag.gotoNormal();
							}
							else if (id.endsWith("queset")) {
								if (GameManager.getInstance().m_role.isAliveStatus()) {
									GameManager.getInstance().m_game_message.sendTaskRoleList();
								}
							}
							else if (id.endsWith("dailyquest")) {
								if (GameManager.getInstance().m_role.isAliveStatus()) {
									GameManager.getInstance().m_game_message.sendOpenActivity();
								}
							}
							else if (id.endsWith("instance")) {//副本
								if (GameManager.getInstance().m_role.isAliveStatus()) {
									GameManager.getInstance().m_game_message.sendGameCarbonOpen();
								}
							}
							else if (id.endsWith("activity")) {
								if (GameManager.getInstance().m_role.isAliveStatus()) {
									GameManager.getInstance().m_game_message.sendOpenActivity();
								}
							}
							else if (id.endsWith("family")) {
								if (GameManager.getInstance().m_role.isAliveStatus()) {
									GameManager.getInstance().m_game_message.sendFamilyOwnMessage();
								}
							}
							break;
						default:
							break;
					}

				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					AbsHyperLinker hyperLinker = m_detail[getLabelIndex()][getItemIndex()];
					if (hyperLinker == null) {
						return;
					}
					hyperLinker.actionOnUp();
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					AbsHyperLinker hyperLinker = m_detail[getLabelIndex()][getItemIndex()];
					if (hyperLinker == null) {
						return;
					}
					hyperLinker.actionOnDown();
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_D)) {
					setLabelIndex(getLabelIndex() - 1);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_B)) {
					setLabelIndex(getLabelIndex() + 1);
				}
				else {
					shortcut();
				}
				break;
		}
	}

	private void transform(final String id){
		final UIMessageBox msg = (UIMessageBox) UIManager.getInstance().createWindow(
				UIWindowFactory.WINDOW_MESSAGEBOX);
		msg.setMessage("提示", "确定", "返回", "直接传送需要消耗神行符一枚，是否传送？");
		msg.addListener(new UIListener() {

			public void actionPerformed()
			{
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					GameManager.getInstance().m_game_message.sendDelivery(id);
					UIManager.getInstance().removeWindow(msg);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					UIManager.getInstance().removeWindow(msg);

				}

			}
		});
	}
	
	private void shortcut()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY1)) {
			key(0);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY2)) {
			key(1);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY3)) {
			key(2);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY4)) {
			key(3);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY5)) {
			key(4);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY6)) {
			key(5);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY7)) {
			key(6);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY8)) {
			key(7);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_KEY9)) {
			key(8);
		}
	}

	private void key(int index)
	{
		if (m_labels == null) {
			return ;
		}
		if (index < 0) {
			return ;
		}
		if (index >= m_labels.length) {
			return ;
		}
		setLabelIndex(index) ;
	}

	private void closeGonglue()
	{
		if (m_state == STATE_ITEM) {
			close();
		}
		else if (m_state == STATE_DETAIL) {
			if (m_items[getLabelIndex()] == null) {
				close();
			}
			else {
				setWindowStatus(STATE_ITEM);
			}
		}
	}

	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		int[][]rect = new int[][]{
				{179,39,28,20},
				{210,39,28,20},
				{240,39,28,20},
				{270,39,28,20},
		};
		initTouchRect(rect);
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		setLabelIndex(touchid);
	}

	
}
